﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using buildingLib.xcategory;
using System.Drawing.Drawing2D;

namespace DocPrinter
{
    public class ItemPrinter
    {
        static int[] contentPos = new int[7] { 0, 400, 600, 800, 1000, 1200, 1900 };
        static Point GetOutputPoint(int row, int col, Point leftTop)
        {
            return new Point(leftTop.X + (contentPos[col] + contentPos[col + 1]) / 2, leftTop.Y + 80 * row + 15);
        }
        static public void PrintTicket(Graphics graphics, XIOTicket ticket)
        {
            #region 设置字体
            Font titleFont = new Font("宋体", 16);
            Font vtitleFont = new Font("宋体", 11);
            Font contentFont = new Font("宋体", 11);
            #endregion
            #region 设置为打印单位
            graphics.PageUnit = GraphicsUnit.Millimeter;
            graphics.PageScale = 0.1F;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            #endregion
            #region 初始化画刷
            SolidBrush brush = new SolidBrush(Color.Black);
            Pen pen = new Pen(Color.Black);
            StringFormat format = new StringFormat();
            Point leftTop = new Point(100, 20);
            int width = 1900;//19cm
            #endregion
            //设置字体信息
            string title = (ticket.TypeID == 0?"清润家园物品入库单":"清润家园物品出库单");
            #region 打印台头
            //打印标题、单号
            format.Alignment = StringAlignment.Center;
            graphics.DrawString(title, titleFont, brush, new Point(leftTop.X + width / 2, leftTop.Y), format);
            format.Alignment = StringAlignment.Far;
            graphics.DrawString("单据编号: "+ticket.SSID, vtitleFont, brush, new Point(leftTop.X + width, leftTop.Y), format);
            //打印科目、对方科目、日期、等信息等信息
            format.Alignment = StringAlignment.Near;
            graphics.DrawString("科目:  " + ticket.Name , vtitleFont, brush, new Point(leftTop.X, leftTop.Y + 70), format);
            format.Alignment = StringAlignment.Center;
            graphics.DrawString("发单日期:" + ticket.RegDate.ToString("D"), vtitleFont, brush, new Point(leftTop.X + (width / 2), leftTop.Y + 70), format);
            format.Alignment = StringAlignment.Far;
            graphics.DrawString("对方科目:  " + ticket.OtherName, vtitleFont, brush, new Point(leftTop.X + width, leftTop.Y + 70), format);
            #endregion
            #region 打印表格
            //表格边框
            Point content = new Point(leftTop.X, leftTop.Y + 120);
            graphics.DrawRectangle(pen, content.X, content.Y, width, 640);

            //绘制线框
            for (int i = 1; i < 8; ++i)
            {
                graphics.DrawLine(pen, content.X, content.Y + i * 80, content.X + width, content.Y + i * 80);
            }
            graphics.DrawLine(pen, content.X + contentPos[1], content.Y, content.X + contentPos[1], content.Y + 560);
            graphics.DrawLine(pen, content.X + contentPos[2], content.Y, content.X + contentPos[2], content.Y + 560);
            graphics.DrawLine(pen, content.X + contentPos[3], content.Y, content.X + contentPos[3], content.Y + 560);
            graphics.DrawLine(pen, content.X + contentPos[4], content.Y, content.X + contentPos[4], content.Y + 560);
            graphics.DrawLine(pen, content.X + contentPos[5], content.Y, content.X + contentPos[5], content.Y + 560);

            //表头内容
            format.Alignment = StringAlignment.Center;
            graphics.DrawString("名      称", contentFont, brush, GetOutputPoint(0, 0, content), format);
            graphics.DrawString("单位", contentFont, brush, GetOutputPoint(0, 1, content), format);
            graphics.DrawString("数量", contentFont, brush, GetOutputPoint(0, 2, content), format);
            graphics.DrawString("单价", contentFont, brush, GetOutputPoint(0, 3, content), format);
            graphics.DrawString("金额", contentFont, brush, GetOutputPoint(0, 4, content), format);
            graphics.DrawString("用途或说明", contentFont, brush, GetOutputPoint(0, 5, content), format);

            //表格内容
            int InitRow = 1;

            foreach (XIOItem fi in ticket.DetailItems)
            {
                graphics.DrawString(fi.Name, contentFont, brush, GetOutputPoint(InitRow, 0, content), format);
                graphics.DrawString(fi.Unit, contentFont, brush, GetOutputPoint(InitRow, 1, content), format);
                graphics.DrawString(fi.Count.ToString(), contentFont, brush, GetOutputPoint(InitRow, 2, content), format);
                graphics.DrawString(fi.Price.ToString("F2"), contentFont, brush, GetOutputPoint(InitRow, 3, content), format);
                graphics.DrawString(fi.TotalPrice.ToString("F2"), contentFont, brush, GetOutputPoint(InitRow, 4, content), format);
                graphics.DrawString(fi.Memo, contentFont, brush, GetOutputPoint(InitRow, 5, content), format);
                //递增
                InitRow++;
            }

            //打印备注
            graphics.DrawString("备注" + ticket.Memo, contentFont, brush, GetOutputPoint(7,0,content), format);
            #endregion

            #region 打印表尾
            Point footer = new Point(content.X, content.Y + 660);
            Rectangle footerR = new Rectangle(footer.X + 100, footer.Y, width - 100, 200);
            format.Alignment = StringAlignment.Near;
            graphics.DrawString("登记人: " + ticket.Logger, vtitleFont, brush, footer, format);
            graphics.DrawString("签  名: " , vtitleFont, brush, new Point(footer.X + 300, footer.Y), format);
            graphics.DrawString("库管员: " , vtitleFont, brush, new Point(footer.X + 600, footer.Y), format);
            graphics.DrawString("会  计: " , vtitleFont, brush, new Point(footer.X + 1200, footer.Y), format);
            #endregion
        }
    }
}
